package com.example.auth.common;

import lombok.Data;

import java.io.Serializable;

/**
 * 统一响应结果
 */
@Data
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 返回消息
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    public static <T> R<T> ok() {
        return restResult(null, ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    public static <T> R<T> ok(T data, String message) {
        return restResult(data, ResultCode.SUCCESS.getCode(), message);
    }

    public static <T> R<T> failed() {
        return restResult(null, ResultCode.FAILURE.getCode(), ResultCode.FAILURE.getMessage());
    }

    public static <T> R<T> failed(String message) {
        return restResult(null, ResultCode.FAILURE.getCode(), message);
    }

    public static <T> R<T> failed(int code, String message) {
        return restResult(null, code, message);
    }

    private static <T> R<T> restResult(T data, int code, String message) {
        R<T> result = new R<>();
        result.setCode(code);
        result.setData(data);
        result.setMessage(message);
        return result;
    }

    /**
     * 响应码枚举
     */
    public enum ResultCode {
        /**
         * 操作成功
         */
        SUCCESS(200, "操作成功"),
        /**
         * 操作失败
         */
        FAILURE(500, "操作失败"),
        /**
         * 未授权
         */
        UNAUTHORIZED(401, "未授权"),
        /**
         * 禁止访问
         */
        FORBIDDEN(403, "禁止访问");

        private final int code;
        private final String message;

        ResultCode(int code, String message) {
            this.code = code;
            this.message = message;
        }

        public int getCode() {
            return code;
        }

        public String getMessage() {
            return message;
        }
    }
} 